//
//  RecommendViewController.m
//  BaseProject
//
//  Created by hundredsandc on 2017/10/25.
//  Copyright © 2017年 hundredsandc. All rights reserved.
//

#import "RecommendViewController.h"
#import "RecommendTableViewCell.h"
#import "HTTPManagers.h"
#import "WEBViewController.h"

@interface RecommendViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UISegmentedControl *segment;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArr;

@end

@implementation RecommendViewController
{
    NSInteger page;
    NSInteger type;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    type = 9;
    page = 1;
    [self getData];
    self.navigationItem.titleView = self.segment;
    [self.view addSubview:self.tableView];
}
-(void)getData{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"channel_id"]=@(type);
    params[@"access_token"] = @"";
    params[@"page_index"] = @(page);
    params[@"page_size"]= @"20";
    params[@"note_type"] = @"10";
    
    [HTTPManagers postNBAFootBallWithURL:@"new_notes_info_query" params:params success:^(id json) {
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        NSDictionary *dict = [json objectForKey:@"new_notes_info_query"];
        NSArray *arr = dict[@"data"];
        for (int i = 0; i<arr.count; i++) {
            NSDictionary *dict = arr[i];
            [self.dataArr addObject:dict];
        }
        [_tableView setContentOffset:CGPointMake(0, 0)];
        [_tableView reloadData];
    } failure:^(NSError *error) {
        
    }];

}
-(void)changeSegment:(UISegmentedControl *)Seg{
    NSInteger Index = Seg.selectedSegmentIndex;
    [self changeLive:Index];
    [self getData];
}
-(void)changeLive:(NSInteger)index{
    if (self.dataArr.count>0) {
        [self.dataArr removeAllObjects];
    }
    if (index==0) {
        type =9;
    }else if(index==1){
        type = 12;
    }else{
        type = 15;
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor whiteColor];
    return view;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dict = self.dataArr[indexPath.section];
    WEBViewController *web = [WEBViewController new];
    if (type==0) {
        web.url = dict[@"info_url"];
    }else{
        web.url = dict[@"share_url"];
    }
    [self.navigationController pushViewController:web animated:YES];
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 75;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RecommendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([RecommendTableViewCell class]) forIndexPath:indexPath];
    if (type==0) {
        if (self.dataArr.count>0) {
            NSDictionary *dict = self.dataArr[indexPath.section];
            [cell customLeftImageUrlStr:dict[@"info_pic"] type:dict[@"info_type_name"] title:dict[@"info_title"] time:dict[@"info_time"]];
        }
    }else{
        if (self.dataArr.count>0) {
            NSDictionary *dict = self.dataArr[indexPath.section];
            [cell customLeftImageUrlStr:dict[@"share_pic"] type:dict[@"channel_name"] title:dict[@"note_title"] time:dict[@"crt_time"]];
        }
    }
    
    return cell;
    
    
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-FTab) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([RecommendTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([RecommendTableViewCell class])];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            if (self.dataArr.count>0) {
                [self.dataArr removeAllObjects];
            }
            [self getData];
            
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            page++;
            [self getData];
            
        }];

    }
    return _tableView;
}
-(UISegmentedControl *)segment{
    if (!_segment) {
        _segment = [[UISegmentedControl alloc]initWithItems:@[@"中超",@"英超",@"欧冠"]];
        UIFont *font = [UIFont boldSystemFontOfSize:16.0f];
        [_segment setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:font} forState:UIControlStateSelected];
        [_segment setTitleTextAttributes:@{NSFontAttributeName:font} forState:UIControlStateNormal];
        _segment.size = CGSizeMake(231, 28);
        _segment.selectedSegmentIndex = 0;
        _segment.tintColor = RGBCOLOR(0x25, 0x43, 0x7f);
        [_segment addTarget:self action:@selector(changeSegment:) forControlEvents:UIControlEventValueChanged];
    }
    return _segment;
}
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

@end
